/*
 * Copyright © 2012 by PropSage Pte Ltd. 
 * All Rights Reserved.
 * licensing@propsage.com
 * http://www.propsage.com/terms.html
 * 
 * @author Poh Nean
 */
propsage.namespace("propsage.brochure.widgets");

propsage.brochure.widgets.Textbox = propsage.brochure.Widget.extend({
	type: 'Textbox',
	template: '<div class="widget-placeholder textbox-placeholder" />',
	selectable: true,
	resizable: true,
	draggable: true,
	rotatable: true,
	setup: function() {},
	init: function(data) {		
		if (data.options.text == null) {
			data.options.text = 'Enter Text...';
		}
		
		this._super(data);
		
		this.widget.prop('contenteditable', true);
		
		Aloha.jQuery(this.widget).aloha();
	},
	getContents: function() {	
		return this.options.text;
	},
	onSelected: function(e, ui) {
		if (!this.isSelected) {
			this._super();
		}
	},
	onUnselected: function(e, ui) {
		this.widget.blur();
		
		this._super();
	},
	handleActions: {
		close: {
			title: 'Delete Textbox',
			click: function(e) {
				var confirm = window.confirm('Delete Textbox?');

				if (confirm) {
					this.destroy();
				}
			}
		}
	},
	events: {
		keydown: function(e) {
			var self = this, o = this.options;
			setTimeout(function() {
				o.text = self.widget.html();
			}, 5);
		},
		mouseenter: function(e) {
			var self = this;
			
			this.widget.bind('mousedown', function(e) {
				// prevent selectable from trigger so that text can be highlighted.
				e.stopImmediatePropagation();
				
				// and then manually trigger the selection
				self.triggerSelect(e);
			});
		}
	}
});